<template>
    <div>
        <div class="addClass">
            <el-input size="small" placeholder="请输入" v-model="role.name">
                <template slot="prepend">搜索</template>
            </el-input>
            <el-input size="small" placeholder="请输入" v-model="role.nameZh" @keydown.enter.native="addRole">
            </el-input>
            <el-button  size="small" type="primary" icon="el-icon-plus" @click="addRole">添加</el-button>
        </div>


        <div class="collapseClass">
            <el-collapse v-model="activeName" accordion @change="change">

                <el-collapse-item :title="grade+'届'" :name="grade" v-for="(grade,index) in grades" :key="index">
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>该届培养计划</span>
                        </div>
                        <div>
                            <el-tree :data="allGradesData"></el-tree>
                        <div style="display: flex;justify-content: flex-end">
                            <el-button size="mini" @click="cancelUpdate">取消修改</el-button>
                            <el-button size="mini" type="primary" @click="update(role.id,index)">确认修改</el-button>
                        </div>
                        </div>
                    </el-card>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
    export default {
        name: "TrainCourseManage",
        data(){
            return {
                activeName: -1,
                role: {
                    name: '',
                    nameZh: '',
                },
                grades: [],
                allGradesData: [{
                    label: '所有',
                    children: [{
                        label: '大一上学期',
                        children: []
                    },{
                        label: '大一下学期',
                        children: []
                    },{
                        label: '大二上学期',
                        children: []
                    },{
                        label: '大二下学期',
                        children: []
                    },{
                        label: '大三上学期',
                        children: []
                    },{
                        label: '大三下学期',
                        children: []
                    },{
                        label: '大四上学期',
                        children: []
                    },{
                        label: '大四下学期',
                        children: []
                    }]
                }],
                selectMenus: [],
                traRequest: {
                    "courseId": "",
                    "courseName": "",
                    "grade": 0
                }

            }
        },
        mounted() {
            this.initGrades();
        },
        methods:{
            addRole(){
              // if(this.role.name && this.role.nameZh){
              //     this.postRequest('/role/role',this.role).then(resp=>{
              //         if(resp){
              //             this.initGrades();
              //               this.role.name='';
              //               this.role.nameZh='';
              //         }
              //     })
              // }  else{
              //     this.$message("所有字段不能为空");
              // }
            },
            change(rid){
                if(rid){
                    this.initAllMenus(rid)
                }
            },
            initAllMenus(rid){
                // 查询所有培养计划
                this.traRequest.grade = rid;
                this.postRequest('/training-program/findAll/1/999',this.traRequest).then(resp=>{
                    if(resp){
                        for (let start = 0; start < 8; start++) {
                            this.allGradesData[0].children[start].children=[];
                            if(resp.data[(start+1)+""]){
                                for(let i = 0; i< resp.data[(start+1)+""].length ;i ++){
                                    let label = {
                                        label: resp.data[(start+1)+""][i].courseName,
                                    }
                                    this. allGradesData[0].children[start].children.push(label)
                                }
                            }
                        }
                    }
                })
            },
            initGrades(){
                this.getRequest('/training-program/selectGrade').then(resp=>{
                    if(resp){
                        this.grades = resp.data;
                    }
                })
            },
            cancelUpdate(){
              this.activeName = -1;
            },
            update(rid,index){
                let tree = this.$refs.tree[index];
                // console.log(tree);
                let selectKeys = tree.getCheckedKeys(true);
                // console.log(selectKeys);
                // console.log(rid);
                let url = '/system/config/updateMenuRole?rid='+rid;
                selectKeys.forEach(key=>{
                    url+='&mids='+key;
                });
                // 调用
                this.putRequest(url).then(resp=>{
                    if(resp){
                        this.activeName = -1;
                    }
                })
            }
        }
    }
</script>

<style scoped>
    .addClass{
        display: flex;
        justify-content: flex-start;
    }
    .addClass .el-input{
        width: 300px;
        margin-right: 6px;
    }
    .collapseClass{
        margin-top: 10px;
        width: 700px;
    }
</style>